﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Stone.DataContracts;
using Stone.Library.DB;

namespace Stone.ServiceImpl.DAO
{
    class XtgnDAO
    {
        public int yh_xtgn_code;
        public int e_right_id;
        public string yh_xtgn_name;
        public string yh_xtgn_column;
        #region  成员方法

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public XtgnEntity XtgnEntity(int yh_xtgn_code)
        {
            XtgnEntity xe = new XtgnEntity();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select yh_xtgn_code,e_right_id,yh_xtgn_name,yh_xtgn_column ");
            strSql.Append(" FROM yh_xtgn ");
            strSql.Append(" where yh_xtgn_code=@yh_xtgn_code ");
            SqlParameter[] parameters = {
					new SqlParameter("@yh_xtgn_code", SqlDbType.Int,4)};
            parameters[0].Value = yh_xtgn_code;

            DataTable dt = DBExecuteUtil.querySqlTable(strSql.ToString(), parameters);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["yh_xtgn_code"].ToString() != "")
                {
                    yh_xtgn_code = int.Parse(dt.Rows[0]["yh_xtgn_code"].ToString());
                }
                if (dt.Rows[0]["e_right_id"].ToString() != "")
                {
                    e_right_id = int.Parse(dt.Rows[0]["e_right_id"].ToString());
                }
                xe.yh_xtgn_name = dt.Rows[0]["yh_xtgn_name"].ToString();
                xe.yh_xtgn_column = dt.Rows[0]["yh_xtgn_column"].ToString();
            }
            return xe;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into yh_xtgn(");
            strSql.Append("e_right_id,yh_xtgn_name,yh_xtgn_column)");
            strSql.Append(" values (");
            strSql.Append("@e_right_id,@yh_xtgn_name,@yh_xtgn_column)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@e_right_id", SqlDbType.Int,4),
					new SqlParameter("@yh_xtgn_name", SqlDbType.VarChar,50),
					new SqlParameter("@yh_xtgn_column", SqlDbType.VarChar,1000)};
            parameters[0].Value = e_right_id;
            parameters[1].Value = yh_xtgn_name;
            parameters[2].Value = yh_xtgn_column;

            object obj = DBExecuteUtil.querySqlTable(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update yh_xtgn set ");
            strSql.Append("e_right_id=@e_right_id,");
            strSql.Append("yh_xtgn_name=@yh_xtgn_name,");
            strSql.Append("yh_xtgn_column=@yh_xtgn_column");
            strSql.Append(" where yh_xtgn_code=@yh_xtgn_code ");
            SqlParameter[] parameters = {
					new SqlParameter("@yh_xtgn_code", SqlDbType.Int,4),
					new SqlParameter("@e_right_id", SqlDbType.Int,4),
					new SqlParameter("@yh_xtgn_name", SqlDbType.VarChar,50),
					new SqlParameter("@yh_xtgn_column", SqlDbType.VarChar,1000)};
            parameters[0].Value = yh_xtgn_code;
            parameters[1].Value = e_right_id;
            parameters[2].Value = yh_xtgn_name;
            parameters[3].Value = yh_xtgn_column;

            DBExecuteUtil.executeSqlWithPar(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int yh_xtgn_code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from yh_xtgn ");
            strSql.Append(" where yh_xtgn_code=@yh_xtgn_code ");
            SqlParameter[] parameters = {
					new SqlParameter("@yh_xtgn_code", SqlDbType.Int,4)};
            parameters[0].Value = yh_xtgn_code;

            DBExecuteUtil.executeSqlWithPar(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public XtgnEntity GetModel(int yh_xtgn_code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 yh_xtgn_code,e_right_id,yh_xtgn_name,yh_xtgn_column ");
            strSql.Append(" FROM yh_xtgn ");
            strSql.Append(" where yh_xtgn_code=@yh_xtgn_code ");
            SqlParameter[] parameters = {
					new SqlParameter("@yh_xtgn_code", SqlDbType.Int,4)};
            parameters[0].Value = yh_xtgn_code;

            DataTable dt = DBExecuteUtil.querySqlTable(strSql.ToString(), parameters);
            XtgnEntity model = new XtgnEntity();
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["yh_xtgn_code"].ToString() != "")
                {
                    model.yh_xtgn_code = int.Parse(dt.Rows[0]["yh_xtgn_code"].ToString());
                }
                if (dt.Rows[0]["e_right_id"].ToString() != "")
                {
                    model.e_right_id = int.Parse(dt.Rows[0]["e_right_id"].ToString());
                }
                model.yh_xtgn_name = dt.Rows[0]["yh_xtgn_name"].ToString();
                model.yh_xtgn_column = dt.Rows[0]["yh_xtgn_column"].ToString();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM yh_xtgn ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DBExecuteUtil.querySqlTable(strSql.ToString());
        }

        #endregion  成员方法
    }
}
